package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign.ArticleFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.usian.common.constants.behavior.FollowBehaviorConstants.*;


@Service
public class UserRelationServiceImpl extends BaseServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserRelationService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private ApUserFanMapper fanMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    @Transactional
    public ResponseResult follow(UserRelationDto dto) {
        //1.参数校验
        if (dto.getAuthorId() == null || (dto.getOperation() != 0 && dto.getOperation() != 1)) return paramError;
        ApAuthor author = articleFeign.findById(dto.getAuthorId());
        if (author == null || author.getUserId() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "关注人不存在");
        //2.获取作者
        Integer followId = author.getUserId();
        Integer currentUserId = AppThreadLocalUtils.getUser().getId();

        return dto.getOperation() == 0 ? relation(followId, currentUserId, dto.getArticleId()) : cancelRelation(followId, currentUserId);
    }

    private ResponseResult cancelRelation(Integer followId, Integer currentUserId) {
        ApUserFollow follow = baseMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().
                eq(ApUserFollow::getUserId, currentUserId).eq(ApUserFollow::getFollowId, followId));
        if (follow != null) {//去取消关注
            baseMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, currentUserId).eq(ApUserFollow::getFollowId, followId));
            fanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, currentUserId).eq(ApUserFan::getUserId, followId));
            return ResponseResult.okResult("取消关注成功");
        } else return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "未关注");

    }

    private ResponseResult relation(Integer followId, Integer currentUserId, Long articleId) {
        ApUserFollow follow = baseMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().
                eq(ApUserFollow::getUserId, currentUserId).eq(ApUserFollow::getFollowId, followId));
        ApUser apUser = apUserMapper.selectById(currentUserId);//当前登录的用户信息
        if (follow == null) {//没有关注记录，进行关注
            ApUserFan fan = new ApUserFan();
            fan.setUserId(followId);
            fan.setFansId(currentUserId.longValue());
            fan.setFansName(apUser.getName());
            fan.setLevel((short) 0);
            fan.setIsDisplay(true);
            fan.setIsShieldLetter(false);
            fan.setIsShieldComment(false);
            fan.setCreatedTime(new Date());
            fanMapper.insert(fan);
            ApUserFollow follows = new ApUserFollow();
            //保存app用户关注信息
            follows.setFollowId(followId);
            follows.setUserId(currentUserId);
            follows.setFollowId(followId);
            follows.setFollowName(apUser.getName());
            follows.setCreatedTime(new Date());
            follows.setIsNotice(true);
            follows.setLevel((short) 1);
            baseMapper.insert(follows);
            kafkaTemplate.send(FOLLOW_BEHAVIOR_TOPIC, followId + "," + currentUserId + "," + articleId);
            return ResponseResult.okResult("关注成功");
        } else return ResponseResult.okResult("该作者已经关注了");
    }
}


























